package top.weiliang.springboot3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.weiliang.springboot3.base.PageBean;
import top.weiliang.springboot3.base.R;
import top.weiliang.springboot3.dao.TbSoftwareTypeMapper;
import top.weiliang.springboot3.entity.TbSoftwareType;
import top.weiliang.springboot3.service.TbSoftwareTypeService;
import top.weiliang.springboot3.vo.PageVO;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbSoftwareTypeServiceImpl implements TbSoftwareTypeService {
    private final TbSoftwareTypeMapper tbSoftwareTypeMapper;

    public TbSoftwareType queryByTname(String tname){
        QueryWrapper<TbSoftwareType> wrapper = new QueryWrapper<>();
        wrapper.eq("tname", tname);
        TbSoftwareType tbSoftwareType = tbSoftwareTypeMapper.selectOne(wrapper);
        return tbSoftwareType;
    }

    @Override
    public R<String> insert(TbSoftwareType tbSoftwareType){
        TbSoftwareType check = queryByTname(tbSoftwareType.getTname());
        if (check != null){
            return R.fail("软件分类已存在",null);
        }

        int i =tbSoftwareTypeMapper.insert(tbSoftwareType);
        return i==1 ?R.ok("添加成功",null):R.fail("添加失败",null);
    }

    @Override
    public R<List<TbSoftwareType>>query(TbSoftwareType tbSoftwareType){
        QueryWrapper<TbSoftwareType> wrapper = new QueryWrapper<>();

        wrapper.like(StringUtils.hasText(tbSoftwareType.getTname()), "tname", tbSoftwareType.getTname());
        wrapper.like(StringUtils.hasText(tbSoftwareType.getIntro()), "intro", tbSoftwareType.getIntro());
        List<TbSoftwareType>list=tbSoftwareTypeMapper.selectList(wrapper);
        return R.ok(list);
    }

    @Override
    public R<TbSoftwareType>queryById(Integer id) {
        TbSoftwareType tbSoftwareType=tbSoftwareTypeMapper.selectById(id);
        return tbSoftwareType==null ?R.ok("查询失败",null) :R.ok(tbSoftwareType);
    }

    @Override
    public R<String>update(TbSoftwareType tbSoftwareType){
        //效验名称是否存在
        TbSoftwareType check = queryByTname(tbSoftwareType.getTname());
        if (check != null && !check.getId().equals(tbSoftwareType.getId())){
            return R.fail("班级名称已存在",null);
        }

        int i=tbSoftwareTypeMapper.updateById(tbSoftwareType);
        return i==1 ?R.ok("修改成功",null) :R.fail("修改失败",null);
    }

    @Override
    public R<String>delete(Integer id){
        int i=tbSoftwareTypeMapper.deleteById(id);
        return i==1 ?R.ok("删除成功",null) :R.fail("删除失败",null);
    }

    @Override
    public R<PageVO<TbSoftwareType>>queryPage(PageBean pageBean, TbSoftwareType tbSoftwareType){
        IPage<TbSoftwareType> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        QueryWrapper<TbSoftwareType> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(tbSoftwareType.getTname()), "tname", tbSoftwareType.getTname());
        wrapper.like(StringUtils.hasText(tbSoftwareType.getIntro()), "intro", tbSoftwareType.getIntro());
        page = tbSoftwareTypeMapper.selectPage(page,wrapper);
        return R.ok(page);
    }
}
